package com.speech.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.List;

/**
 * Created by XuLH on 2016/5/13.
 */

public class JsonProcessor {
    //定义为一个内部变量，不每次new
    //但是也绝对不能使用 static
    private ObjectMapper jacksonObjectMapper = new ObjectMapper();

    private static ObjectMapper staticMapper = new ObjectMapper();

    static {
        staticMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        staticMapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
        staticMapper.configure(MapperFeature.AUTO_DETECT_CREATORS, false);
        staticMapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
        staticMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
        staticMapper.configure(SerializationFeature.CLOSE_CLOSEABLE, true);

        //默认不写null值
        staticMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public JsonProcessor() {
        jacksonObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jacksonObjectMapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
        jacksonObjectMapper.configure(MapperFeature.AUTO_DETECT_CREATORS, false);
        jacksonObjectMapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
        jacksonObjectMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
        jacksonObjectMapper.configure(SerializationFeature.CLOSE_CLOSEABLE, true);
    }

    public <T> String getJson(T obj) {
        String str_ret = "";
        try {
            str_ret = jacksonObjectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str_ret;
    }

    public <T> T getObject(Class<T> c, String str) {
        T ret = null;
        if (str != null) {
            try {
                ret = staticMapper.readValue(str, c);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public <T> List<T> getList(String str) {
        List<T> ret = null;
        if (str != null) {
            try {
                ret = jacksonObjectMapper.readValue(str, new TypeReference<List<T>>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    public static <T> String StcGetJson(T obj) {
        String str_ret = "";
        try {
            if (obj != null) {
                str_ret = staticMapper.writeValueAsString(obj);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str_ret;
    }

    public static <T> T StcGetObject(Class<T> c, String str) {
        T ret = null;
        try {
            if (str != null && str.length() > 0) {
                ret = staticMapper.readValue(str, c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static <T> List<T> StcGetList(Class<T> c, String str) {
        List<T> ret = null;
        try {
            if (str != null && str.length() > 0) {
                JavaType javaType = staticMapper.getTypeFactory().constructParametricType(List.class, c);
                ret = staticMapper.readValue(str, javaType);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

}

